package com.step.scene.fog;

import javax.microedition.khronos.opengles.GL10;

import com.step.mesh.Color;

public class Fog {
	float[] color;
	float far;
	float near;
	FogType fogType;
	
	public Fog(){
		this.color = new float[]{1, 1, 1, 1};
		this.far = 10;
		this.near = 0;
		this.fogType = FogType.LINEAR;
	}
	
	public void setColor(int r, int g, int b, int a){
		color[0] = (float)r / 255;
		color[1] = (float)g / 255;
		color[2] = (float)b / 255;
		color[3] = (float)a / 255;
	}
	
	public void setColor(float r, float g, float b, float a){
		color[0] = r;
		color[1] = g;
		color[2] = b;
		color[3] = a;
	}
	
	public void setColor(Color color){
		this.color[0] = (float)color.r / 255f;
		this.color[1] = (float)color.g / 255f;
		this.color[2] = (float)color.b / 255f;
		this.color[3] = (float)color.a / 255f;
	}
	
	public void setNear(float near){
		this.near = near;
	}
	
	public void setFar(float far){
		this.far = far;
	}
	
	public void setNearAndFar(float near, float far){
		setNear(near);
		setFar(far);
	}
	
	public void setFogType(FogType type){
		this.fogType = type;
	}
	
	public void enable(GL10 gl){
		gl.glFogf(GL10.GL_FOG_MODE, fogType.glValue());
		gl.glFogf(GL10.GL_FOG_START, near);
		gl.glFogf(GL10.GL_FOG_END, far);
		gl.glFogfv(GL10.GL_FOG_COLOR, color, 0);
		gl.glEnable(GL10.GL_FOG);
	}
	
	public void disable(GL10 gl){
		gl.glDisable(GL10.GL_FOG);
	}
	
	public enum FogType{
		LINEAR(GL10.GL_LINEAR),
		EXP(GL10.GL_EXP),
		EXP2(GL10.GL_EXP);
		
		private final int glValue;
		
		private FogType(int glValue){
			this.glValue = glValue;
		}
		
		public int glValue(){
			return glValue;
		}
	}
}
